/***************************************************************************
 *   Copyright (C) 2007 by Sebastian Bilski, Lance Cooper, Jeff Rebacz,
 *   									and Alex Zelenskiy
 *   sbilski@iit.edu
 *   lance.m.cooper@gmail.com
 *   jeff.rebacz@gmail.com
 *   voiceofanarchy@gmail.com
 *
 *	This file is part of connect3
 *
 *   connect3 is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2,
 *    as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc., 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 ***************************************************************************/

import java.awt.Color;

import junit.framework.TestCase;


public class Test_GameBoard extends TestCase {
	
	GameBoard gb1;
	GameBoard gb2;
	GameBoard gb3;
	
	Player p1;
	Player p2;
	
	GamePiece t1;
	GamePiece t2;
	GamePiece t3;
	GamePiece t4;
	
	public Test_GameBoard(String name){
		super(name);
	}	
	
	protected void setUp(){
		gb1 = new GameBoard();
		gb2 = new GameBoard(40,20);
		gb3 = new GameBoard(-4,-123);
		
		p1 = new Player("p1", Color.GREEN);
		p2 = new Player("p2", Color.YELLOW);
		
		t1 = p1.generatePiece();
		t2 = p1.generatePiece();
		t3 = p2.generatePiece();
		t4 = p2.generatePiece();
	}
	
	
	protected void tearDown(){
	}
	
	public void testDefaultConstructor(){
		assertTrue(gb1.getXDimension()==1);
		assertTrue(gb1.getYDimension()==1);
		assertTrue(gb1.getPieceAt(0,0)==null);		
	}
	
	public void testNonDefaultConstructor(){
		assertTrue(gb2.getXDimension()==40);
		assertTrue(gb2.getYDimension()==20);
		assertTrue(gb2.getPieceAt(0,0)==null);	
		assertTrue(gb2.getPieceAt(39,0)==null);	
		assertTrue(gb2.getPieceAt(0,20)==null);	
		assertTrue(gb2.getPieceAt(39,20)==null);	
		assertTrue(gb2.getPieceAt(5,16)==null);	
		assertTrue(gb2.getPieceAt(-3,16)==null);	
		assertTrue(gb2.getPieceAt(33,33)==null);	
	}
	
	public void testErroneousConstructorCall(){
		assertTrue(gb3.getXDimension()==1);
		assertTrue(gb3.getYDimension()==1);
		assertTrue(gb3.getPieceAt(0,0)==null);
		assertTrue(gb3.getPieceAt(1,1)==null);	
	}
	
	public void testMakeMoveLeftSide(){
		int col=-1;
		col = gb2.makeMove(t1, 0);
		assertTrue(gb2.getPieceAt(0, col)==t1);
		col = gb2.makeMove(t2, 1);
		assertTrue(gb2.getPieceAt(1, col)==t2);
		col = gb2.makeMove(t3, 0);
		assertTrue(gb2.getPieceAt(0, col)==t3);
		col = gb2.makeMove(t4, 1);
		assertTrue(gb2.getPieceAt(1, col)==t4);		
	}
	public void testMakeMoveRightSide(){		
		int col=-1;
		col = gb2.makeMove(t1, 39);
		assertTrue(gb2.getPieceAt(39, col)==t1);
		col = gb2.makeMove(t2, 38);
		assertTrue(gb2.getPieceAt(38, col)==t2);
		col = gb2.makeMove(t3, 39);
		assertTrue(gb2.getPieceAt(39, col)==t3);
		col = gb2.makeMove(t4, 38);
		assertTrue(gb2.getPieceAt(38, col)==t4);
	}
	
	
	//test destroys and resets
	public void testDestroyReset(){
		int col=-1;
		col = gb2.makeMove(t1, 39);
		col = gb2.makeMove(t2, 38);
		col = gb2.makeMove(t3, 39);
		col = gb2.makeMove(t4, 38);
		
		gb2.destroy();
		assertTrue(gb2.getXDimension()==40);
		assertTrue(gb2.getYDimension()==20);
		assertTrue(gb2.getPieceAt(0,0)==null);	
		assertTrue(gb2.getPieceAt(39,0)==null);	
		assertTrue(gb2.getPieceAt(0,20)==null);	
		assertTrue(gb2.getPieceAt(39,20)==null);	
		assertTrue(gb2.getPieceAt(5,16)==null);	
		assertTrue(gb2.getPieceAt(-3,16)==null);	
		assertTrue(gb2.getPieceAt(33,33)==null);	
		
		col = gb2.makeMove(t1, 0);
		assertTrue(gb2.getPieceAt(0, col)==t1);
		col = gb2.makeMove(t2, 1);
		assertTrue(gb2.getPieceAt(1, col)==t2);
		col = gb2.makeMove(t3, 0);
		assertTrue(gb2.getPieceAt(0, col)==t3);
		col = gb2.makeMove(t4, 1);
		assertTrue(gb2.getPieceAt(1, col)==t4);	
		
		gb2.reset();
		assertTrue(gb2.getXDimension()==40);
		assertTrue(gb2.getYDimension()==20);
		assertTrue(gb2.getPieceAt(0,0)==null);	
		assertTrue(gb2.getPieceAt(39,0)==null);	
		assertTrue(gb2.getPieceAt(0,20)==null);	
		assertTrue(gb2.getPieceAt(39,20)==null);	
		assertTrue(gb2.getPieceAt(5,16)==null);	
		assertTrue(gb2.getPieceAt(-3,16)==null);	
		assertTrue(gb2.getPieceAt(33,33)==null);	
		
		col = gb2.makeMove(t1, 0);
		assertTrue(gb2.getPieceAt(0, col)==t1);
		col = gb2.makeMove(t2, 1);
		assertTrue(gb2.getPieceAt(1, col)==t2);
		col = gb2.makeMove(t3, 0);
		assertTrue(gb2.getPieceAt(0, col)==t3);
		col = gb2.makeMove(t4, 1);
		assertTrue(gb2.getPieceAt(1, col)==t4);			
	}
	
	public void testErroneousMoves(){
		assertTrue(gb2.makeMove(t1, -1)==-1);
		assertTrue(gb2.makeMove(t1, 40)==-1);
	}
	
	public void testFilling2x2(){
		gb3.setXDimension(2);
		gb3.setYDimension(2);
		gb3.reset();
		int col;
		col = gb3.makeMove(t1, 0);
		assertTrue(gb3.getPieceAt(0, col)==t1);
		col = gb3.makeMove(t2, 1);
		assertTrue(gb3.getPieceAt(1, col)==t2);
		col = gb3.makeMove(t3, 0);
		assertTrue(gb3.getPieceAt(0, col)==t3);
		col = gb3.makeMove(t4, 1);
		assertTrue(gb3.getPieceAt(1, col)==t4);	
		assertTrue(gb3.makeMove(t4, 0)==-1);	
		assertTrue(gb3.makeMove(t4, 1)==-1);		
	}
	
	public void testCheckMove(){
		gb3 = new GameBoard(1,1);
		assertTrue(GameLogic.checkMove(gb3,0));
		assertFalse(GameLogic.checkMove(gb3,-1));
		assertFalse(GameLogic.checkMove(gb3,1));
		gb3.makeMove(p1.generatePiece(), 0);
		assertFalse(GameLogic.checkMove(gb3,0));
		assertFalse(GameLogic.checkMove(gb3,-1));
		assertFalse(GameLogic.checkMove(gb3,1));
		
	}
}
